﻿package com.projects.xenia;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import com.projects.xenia.Documents_Return.DocLine;
import com.projects.xenia.documents_classes.Documents_Order;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.prompts_activities.Activity_ReturnGoodsPrompt;
import com.projects.xenia.ref_classes.GoodsRefItem;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ReturnContentActivity extends ListActivity {

	private EfficientAdapter adap;	

	private Documents_Return DocumentClass;	

	private Context Ctx;	
	
	public HashMap<String, DocLine> ReturnsDocLineCollection;
	
	private ImageView GoodsSelectOpenImageView;
	private ImageView return_backward;	
	
	private String [] Date_Types = new String [] {"Изготовлен:","Годен до:"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
				
        requestWindowFeature(Window.FEATURE_NO_TITLE);            
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.return_content_form);

		Ctx = this;
		DocumentClass = (Documents_Return) MainGlobalModule.DocumentClass;
		
		GoodsSelectOpenImageView = (ImageView)findViewById(R.id.GoodsSelectOpen);
		GoodsSelectOpenImageView.setOnClickListener(GoodsSelectOpenImageView_OnClick);
		
		return_backward = (ImageView)findViewById(R.id._backward);
		return_backward.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish ();				
			}
		});
		
		
		adap = new EfficientAdapter(Ctx);
		setListAdapter(adap);
		
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long i_id) {
		super.onListItemClick(l, v, position, i_id);
		
		DocumentClass.SetLineDocByPosition (position);		
		Intent ReturnPrompt = new Intent ();		 
		ReturnPrompt.setClass(Ctx,Activity_ReturnGoodsPrompt.class);		
		startActivityForResult(ReturnPrompt, 0);
		
	}
	
	private void OpenGoodsSelection () {
	
		DocumentClass.NewLine (); 
		DocumentClass.Head.state = 1;
		DocumentClass.UpdateDoc();		
		Intent GoodsCatalog = new Intent();
		GoodsCatalog.setClass(this,MainGoodsCatalogActivity.class);
		GoodsCatalog.putExtra("DocType",  MainGlobalModule.DocumentsType.Return);		
		GoodsCatalog.putExtra("DocSelection",  01);
		GoodsCatalog.putExtra("SelectOnce",  true);
		startActivityForResult(GoodsCatalog,0);
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		adap.notifyDataSetChanged();
			
	}
	
	
	private OnClickListener GoodsSelectOpenImageView_OnClick = new OnClickListener() {		
		@Override
		public void onClick(View v) {
			
			OpenGoodsSelection ();			    		
			
		}
	};
	
public class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
	 
		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}
		 
		public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		
		if (convertView == null) {
			
			convertView = mInflater.inflate(R.layout.return_content_list, null);
		
			holder = new ViewHolder();
		
			holder.ReturnGoodTextView = (TextView)convertView.findViewById(R.id.ReturnGoodTextView);
			holder.ReturnMeasureTextView = (TextView)convertView.findViewById(R.id.ReturnMeasureTextView);
			holder.ReturnPriceTextView = (TextView)convertView.findViewById(R.id.ReturnPriceTextView);
			holder.ReturnCountTextView = (TextView)convertView.findViewById(R.id.ReturnCountTextView);
			holder.ReturnSumTextView = (TextView)convertView.findViewById(R.id.ReturnSumTextView);
			holder.ReturnDateTextView = (TextView)convertView.findViewById(R.id.ReturnDateTextView);
			holder.ReturnBaseTextView = (TextView)convertView.findViewById(R.id.ReturnBaseTextView);
		
			convertView.setTag(holder);
		
		}
		
		else
			
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		DocLine dl = getItem(position);
		
		holder.ReturnGoodTextView.setText(dl.goods_descr);
		holder.ReturnMeasureTextView.setText(String.format("%s(%.1f) ", dl.goods_measures_descr ,dl.factor));			
		holder.ReturnPriceTextView.setText(String.format ("ц.%.2f ",dl.price));
		holder.ReturnCountTextView.setText(String.format ("%.2f ",dl.return_value));
		holder.ReturnSumTextView.setText(String.format ("сум.%.2f ",dl.return_sum));		
		holder.ReturnDateTextView.setText(String.format ("%s %td.%tm.%ty ", Date_Types[dl.date_type] ,dl.create_date,dl.create_date,dl.create_date));
		holder.ReturnBaseTextView.setText(String.format ("осн: %s",dl.return_base_descr));
		
		return convertView;
		
		}
		 
		class ViewHolder {
			
			TextView ReturnGoodTextView;
			TextView ReturnMeasureTextView;
			TextView ReturnPriceTextView;
			TextView ReturnCountTextView;
			TextView ReturnSumTextView;
			TextView ReturnDateTextView;
			TextView ReturnBaseTextView;			
					
		}
				 
		@Override
		public long getItemId(int position) {
			
			return 0;
			
		}
		 
		@Override
		public int getCount() {

			return DocumentClass.DocLineItemsCollection.size();
			
		}
		 
		@Override
		public DocLine getItem(int position) {
			
			return DocumentClass.DocLineItemsCollection.get(position);
		
		}
		
	
	}
	
}
